# modules/squid/manifests/init.pp - manage squid stuff
# Copyright (C) 2007 admin@immerda.ch
#

# modules_dir { "nfs": }

class nfs_server {
  package { "nfs-utils":
    ensure => installed,
  }
  
  service { [ "nfs", "nfslock", "portmap"]:
    ensure => running,
    enable => true,
    require => Package["nfs-utils"],
  }
}

class nfs_client {
  package { "nfs-utils":
    ensure => installed,
  }
  service { [ "nfs", "nfslock", "portmap"]:
    ensure => running,
    enable => true,
    require => Package["nfs-utils"],
  }
}

define share_dirs($share_root) {

  notice("mkdir -p $share_root")

  exec { create_share_root:
    command => "mkdir -p $share_root",
  }
  
  notice("$share_root")

  file { "${share_root}":
    ensure => directory,
    owner => "root",
    group => "root",
    mode => 755,
    require => Exec["create_share_root"],
  }
}

define mount_nfs_share($source) {
  notice("mount_nfs_share: name=$name, source=$source")

  include nfs_client

  exec { create_mountpoint:
    command => "mkdir -p $name",
  }
  
  file { $name:
    ensure => directory,
    owner => "root",
    group => "root",
    mode => 755,
    require => Exec["create_mountpoint"],
  }

  mount { $name:
    atboot => true,
    device => $source,
    fstype => "nfs",
    name => $name ,
    options => "defaults,rw,user,exec",
    ensure => "mounted",
    require => File[$name],
  }

}

define unmount_nfs_share($source) {
  notice("unmount_nfs_share: name=$name, source=$source")

  include nfs_client

  mount { $name:
    device => $source,
    name => $name ,
    ensure => "unmounted",
  }

  file { $name:
    ensure  => absent,
  }
}

define nfs_share ($shares, $clientlist = [], $share_access = "*.$domain", $share_options = "rw,sync") {
  include nfs_server
  share_dirs { $name: share_root => $shares }   # $shares is an array
  file { "/etc/exports":
    owner => "root",
    group => "root",
    mode => 644,
    content => template("nfs/etc-exports.tpl"),
    notify => Service["nfs"],
  }
}

